<template>
  <div class="modal">
    <div class="mask" @click="changeIsShowLoginModal(false)"></div>
    <div class="loginbox">
      <div class="close" @click="changeIsShowLoginModal(false)"></div>
      <nav class="nav">
        <a
          href="javascript:;"
          :class="{ active: isShowPhoneLogin }"
          @click="isShowPhoneLogin = true"
          >手机号码登录</a
        >
        |
        <a
          href="javascript:;"
          :class="{ active: !isShowPhoneLogin }"
          @click="showQr"
          >微信扫码登录</a
        >
      </nav>
      <div class="content">
        <div class="phoneLogin" v-show="isShowPhoneLogin">
          <div class="row">
            <input
              type="text"
              placeholder="请输入手机号"
              v-model="phoneNumber"
            />
          </div>
          <div class="row">
            <slide-verify
              :l="42"
              :r="20"
              :w="362"
              :h="140"
              @success="onSuccess"
              @fail="onFail"
              @refresh="onRefresh"
              :style="{ width: '100%' }"
              class="slide-box"
              ref="slideBlock"
              :slider-text="msg"
              :imgs="imgs"
            ></slide-verify>
          </div>
          <div class="row">
            <input
              type="text"
              placeholder="请输入短信验证码"
              v-model="yzmCode"
            />
            <button class="btn btncode" @click="getCode" v-show="!isShowCount">
              获取验证码
            </button>
            <button class="btn btncode" v-show="isShowCount">
              {{ count + "s" }}
            </button>
          </div>
          <div class="row">
            <button class="btn btnLogin" @click="submitFn">登录</button>
          </div>
        </div>
        <div class="qrLogin" id="weixin" v-show="!isShowPhoneLogin">
          22222222222222
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapActions } from "vuex";
import {
  SendSMSAPI,
  PhoneReginAPI,
  BindingAPI,
  UserProfiles,
} from "@/request/api.js";
import { validateTelephoneNumber } from "@/util";
export default {
  data() {
    return {
      isShowPhoneLogin: true,
      phoneNumber: "",
      count: 5,
      isShowCount: false,
      yzmCode: "",
      imgs: [
        require("../assets/img/integral-01.9386d4bf.png"),
        require("../assets/img/integral-03.9870f3f1.png"),
        require("../assets/img/integral-04.afadcbdf.png"),
      ],
      msg: "向右滑动",
    };
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModal: "showModal/changeIsShowLoginModal",
      changeIsLogined: "loginStatus/changeIsLogined",
    }),
    ...mapActions({
      asyncShowToast: "showToast/asyncShowToast",
      asyncChangeUserInfo: "userInfo/asyncChangeUserInfo",
    }),
    async submitFn() {
      //验证登录和滑块
      if (!this.toVerify()) return;
      //判断验证码
      if ((this.yzmCode = "")) {
        alert("请输入短信验证！");
        return;
      }
      //发起登录请求是判断是否有uuid
      let uuid = localStorage.getItem("uuid");
      let res;
      if (uuid) {
        res = await BindingAPI({
          phone: this.phoneNumber,
          verifyCode: this.yzmCode,
          uuid,
        });
        // console.log("绑定手机", res);
      } else {
        //发起登录请求
        res = await PhoneReginAPI({
          verifyCode: this.yzmCode,
          phone: this.phoneNumber,
        });
      }
      if (!res) return;
      // console.log("登录成功", res);
      this.asyncShowToast({
        isShow: true,
        title: "登录成功",
        type: "success",
      });
      //储存token
      localStorage.setItem("x-auth-token", res.data["x-auth-token"]);
      //隐藏登录框
      this.changeIsShowLoginModal(false);
      //更新登录状态
      this.changeIsLogined(true);
      //删除uuid
      localStorage.removeItem("uuid");

      //获取用户信息
      this.asyncChangeUserInfo();
    },
    toVerify() {
      if (!validateTelephoneNumber(this.phoneNumber)) {
        this.asyncShowToast({
          isShow: true,
          title: "请输入手机验证码！",
          type: "danger",
        });
        return;
      }
      if (this.msg == "向右滑动" || this.msg == "再试一次") {
        this.asyncShowToast({
          isShow: true,
          title: "请完成拼图",
          type: "danger",
        });
        return;
      }
      return true;
    },
    async getCode() {
      if (!this.toVerify()) return;
      let res = await SendSMSAPI({
        phone: this.phoneNumber,
      });
      if (!res) return;
      this.isShowCount = true;
      let timer = setInterval(() => {
        this.count--;
        if (this.count == 0) {
          clearInterval(timer);
          this.count = 5;
          this.isShowCount = false;
        }
      }, 1000);
    },
    // 拼图成功
    onSuccess(times) {
      let ms = (times / 1000).toFixed(1);
      this.msg = "login success, 耗时 " + ms + "s";
    },
    // 拼图失败
    onFail() {
      this.onRefresh(); // 重新刷新拼图
    },
    // 拼图刷新
    onRefresh() {
      this.msg = "再试一次";
    },
    //微信扫码登录
    showQr() {
      this.isShowPhoneLogin = false;
      let _this = this;
      new WxLogin({
        id: "weixin",
        appid: "wx67cfaf9e3ad31a0d", // 这个appid要填死
        scope: "snsapi_login",
        // 扫码成功后重定向的接口
        redirect_uri: "https://sc.wolfcode.cn/cms/wechatUsers/shop/PC", //code
        // state填写编码后的url
        state: encodeURIComponent(
          window.btoa("http://127.0.0.1:8080" + _this.$route.path) //code
        ),
        // 调用样式文件
        href: "data:text/css;base64,LmxvZ2luUGFuZWwgLnRpdGxlLA0KLmxvZ2luUGFuZWwgLmluZm8gew0KICAgIGRpc3BsYXk6IG5vbmU7DQp9",
      });
    },
  },
};
</script>

<style lang="less" scoped>
#weixin {
  text-align: center;
}
.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  .mask {
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
  }
  .loginbox {
    position: absolute;
    left: 50%;
    top: 50%;
    margin-left: -277px;
    margin-top: -212px;
    width: 555px;
    height: 423px;
    background: url("../assets/img/login-box-bg.png");
    .close {
      position: absolute;
      top: 15px;
      right: 60px;
      width: 22px;
      height: 22px;
      background: url("../assets/img/close.png");
    }
    .nav {
      margin-top: 45px;
      text-align: center;
      font-style: 16px;
      a {
        text-decoration: none;
        color: black;
        &.active {
          color: blue;
        }
      }
    }
    .content {
      width: 355px;
      margin: 30px auto 0;
      .phoneLogin {
        .row {
          display: flex;
          margin-bottom: 20px;
          input {
            flex: 1;
            height: 50px;
            text-indent: 1em;
            outline: none;
          }
          .btn {
            background: #0a328e;
            color: #fff;
            border: none;
          }
          .btncode {
            margin-left: 10px;
            width: 80px;
          }
          .btnLogin {
            flex: 1;
            height: 50px;
          }
        }
      }
    }
  }
}

/deep/.slide-box {
  width: 100%;
  position: relative;
  box-sizing: border-box;
  canvas {
    position: absolute;
    left: 0;
    top: -120px;
    display: none;
    width: 100%;
    box-sizing: border-box;
  }
  .slide-verify-block {
    width: 85px;
    height: 136px;
  }
  .slide-verify-refresh-icon {
    top: -120px;
    display: none;
  }
  &:hover {
    canvas {
      display: block;
    }
    .slide-verify-refresh-icon {
      display: block;
    }
  }
}
</style>